package com.xgkj.diyiketang.fragment;

import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.xgkj.diyiketang.R;
import com.xgkj.diyiketang.activity.AnchorActivity;
import com.xgkj.diyiketang.activity.data.AnchorListAdapter;
import com.xgkj.diyiketang.data.EatShowUserInfo;
import com.xgkj.diyiketang.http.engine.ShowToastTaskEventListener;
import com.xgkj.diyiketang.http.engine.TaskEventListener;
import com.xgkj.diyiketang.task.AnchorListTask;
import com.xgkj.diyiketang.util.ImageCache;
import com.xgkj.diyiketang.util.ToastUtil;
import com.xgkj.diyiketang.weidget.ImageCycleView;
import com.xgkj.diyiketang.weidget.MyScrollView;
import com.xgkj.diyiketang.weidget.NoScrollGridView;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrater on 2015/12/8.
 */
public class AnchorFragment extends BaseFragment implements View.OnClickListener,MyScrollView.OnScrollListener{
    private Context mContext;
    private NoScrollGridView mGridView;
    private AnchorListAdapter mAdapter;
    private MyScrollView mScrollView;
    private int currentPage = 1;
    private int size = 10;
    private String type = "1";
    private int anchorListTaskId = -1;
    private List<EatShowUserInfo> mList = new ArrayList<EatShowUserInfo>();
    private ImageCycleView adView;
    private int tuijianTaskId = -1;
    private List<EatShowUserInfo> adList = new ArrayList<EatShowUserInfo>();

    @Override
    protected int getContentViewId() {
        return 0;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.anchorfragment,null);
        mContext = getActivity();
        initView(view);

        return view;
    }
    private void initView(View view){
        mGridView = (NoScrollGridView) view.findViewById(R.id.anchor_list);
        adView = (ImageCycleView) view.findViewById(R.id.ad_list);
        mAdapter = new AnchorListAdapter(mContext);
        mGridView.setAdapter(mAdapter);
        mGridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                EatShowUserInfo info = (EatShowUserInfo) mAdapter.getItem(position);
                startActivity(AnchorActivity.getIntent(mContext,info.getUserId()));
            }
        });
        mScrollView = (MyScrollView) view.findViewById(R.id.scrollView);
        mScrollView.setOnScrollListener(this);
        getData();
//        getTuijianAnchor();
    }

    @Override
    public void onClick(View view) {

    }

    @Override
    public void onScroll() {
        getData();
    }

    private void getData(){
        if(currentPage ==0){
            //Toast.makeText(mContext, R.string.no_more, Toast.LENGTH_SHORT).show();
            return ;
        }
        if(anchorListTaskId != -1) {
            return;
        }
            type = "1";
            AnchorListTask task = new AnchorListTask(size, currentPage, "","","",type,null);
            task.addListener(anchorListListener);
            anchorListTaskId = getEatShowApp().getTaskManager().addTask(task);
    }

    private void getTuijianAnchor(){
//        String resp = "[{'userId':1,'userLogo':'http://f.hiphotos.baidu.com/image/h%3D200/sign=67ab903616dfa9ece22e511752d1f754/c75c10385343fbf227695409b77eca8065388f57.jpg','nickName':'小一','fansNum':3230,'isFocus':false},{'userId':2,'userLogo':'http://img0.imgtn.bdimg.com/it/u=4103130088,2567396922&fm=21&gp=0.jpg','nickName':'小二','fansNum':3233,'isFocus':false},{'userId':3,'userLogo':'https://ss0.bdstatic.com/5aV1bjqh_Q23odCf/static/superman/img/logo/bd_logo1_31bdc765.png','nickName':'小三','fansNum':3235,'isFocus':false},{'userId':1,'userLogo':'https://ss0.bdstatic.com/5aV1bjqh_Q23odCf/static/superman/img/logo/bd_logo1_31bdc765.png','nickName':'小一','fansNum':3230,'isFocus':false},{'userId':1,'userLogo':'https://ss0.bdstatic.com/5aV1bjqh_Q23odCf/static/superman/img/logo/bd_logo1_31bdc765.png','nickName':'小一','fansNum':3230,'isFocus':false},{'userId':1,'userLogo':'https://ss0.bdstatic.com/5aV1bjqh_Q23odCf/static/superman/img/logo/bd_logo1_31bdc765.png','nickName':'小一','fansNum':3230,'isFocus':false},{'userId':1,'userLogo':'https://ss0.bdstatic.com/5aV1bjqh_Q23odCf/static/superman/img/logo/bd_logo1_31bdc765.png','nickName':'小一','fansNum':3230,'isFocus':false},{'userId':1,'userLogo':'https://ss0.bdstatic.com/5aV1bjqh_Q23odCf/static/superman/img/logo/bd_logo1_31bdc765.png','nickName':'小一','fansNum':3230,'isFocus':false},{'userId':1,'userLogo':'https://ss0.bdstatic.com/5aV1bjqh_Q23odCf/static/superman/img/logo/bd_logo1_31bdc765.png','nickName':'小一','fansNum':3230,'isFocus':false},{'userId':1,'userLogo':'http://img1.imgtn.bdimg.com/it/u=3282316240,163290015&fm=21&gp=0.jpg','nickName':'小一','fansNum':3230,'isFocus':false}]";
//        adList = JSON.parseArray(resp,EatShowUserInfo.class);
//        ArrayList<String> list = new ArrayList<String>();
//        for(EatShowUserInfo info : adList){
//            list.add(info.getUserLogo());
//        }
//        adView.setImageResources(list,adListener);
        type ="0";
        AnchorListTask tuijianTask = new AnchorListTask(size, 1,"", "","",type,null);
        tuijianTask.addListener(anchorListListener);
        tuijianTaskId = getEatShowApp().getTaskManager().addTask(tuijianTask);
    }

    private ImageCycleView.ImageCycleViewListener adListener = new ImageCycleView.ImageCycleViewListener() {
        @Override
        public void displayImage(String imageURL, ImageView imageView) {
            ImageCache.getInstance(mContext).loadBitmapToView(imageView,imageURL,null);
        }

        @Override
        public void onImageClick(int position, View imageView) {
//            ToastUtil.showMessage(mContext,"点击的是第"+position+"个");
            EatShowUserInfo info = adList.get(position);
            startActivity(AnchorActivity.getIntent(mContext,info.getUserId()));
        }
    };

    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        if(!hidden){
            if(mAdapter != null && mAdapter.getCount() == 0){
                currentPage = 1;
                getData();
            }
        }
    }

    private TaskEventListener anchorListListener = new ShowToastTaskEventListener(getActivity()) {
        @Override
        public void handleTaskCompleted(int taskId, int result, Object data) {
            super.handleTaskCompleted(taskId,  result,  data);
            if(taskId == anchorListTaskId){
                anchorListTaskId = -1;
                if(currentPage == 1){
                    mAdapter.clearData();
                    getTuijianAnchor();
                }
                if(data != null){
                    mList = (List<EatShowUserInfo>) data;
                    if(mList != null && mList.size()>0){
                        mAdapter.updateData(mList);
                    }
                }

                currentPage = result;
                if (currentPage == 0){

                }
            }else if(taskId == tuijianTaskId){
                tuijianTaskId = -1;
                if(data != null){
                    adList = (List<EatShowUserInfo>) data;
                    ArrayList<String> urlList = new ArrayList<String>();
                    for (EatShowUserInfo info : adList){
                        urlList.add(info.getUserLogo());
                    }
                    adView.setImageResources(urlList,adListener);
                }
            }
        }

        @Override
        public void handleTaskError(int taskId, int errorCode, String errorMsg) {
            super.handleTaskError( taskId,  errorCode,  errorMsg);
            if(taskId == anchorListTaskId){
                anchorListTaskId = -1;
            }else if (taskId == tuijianTaskId){
                tuijianTaskId = -1;
            }
        }

        @Override
        public void handleTaskMessage(int taskId, Object data) {

        }
    };
}
